"""
    @Author  ：思念 
    @File    ：6.提取4399页面信息并写入到json中.py
    @Date    ：2024/12/11 17:22 
"""
import jsonpath
import requests
import json
from lxml import etree

url = 'https://www.4399.com/flash_fl/12_1.htm'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
}
response = requests.get(url=url, headers=headers).content.decode('gbk')
tree = etree.HTML(response)
a_list = tree.xpath("//ul[@class='list cf']/li/a")

game_list = []

for a_element in a_list:
    temp = dict()
    temp["game_name"] = a_element.xpath("./text()")[0]
    temp["game_url"] = a_element.xpath("./@href")[0]
    game_list.append(temp)


with open('../gameList.json', 'w', encoding='utf-8') as f:
    # write 无法直接写入python对象
    # 使用json.dumps  indent 缩进 ensure_ascii False 写入中文字符
    f.write(json.dumps(game_list, indent=4, ensure_ascii=False))
